package com.example.cloud.service.system.impl;

import com.example.cloud.constants.StateConstants;
import com.example.cloud.dao.mongdb.system.MongoUserRepository;
import com.example.cloud.dao.system.UserRepository;
import com.example.cloud.model.system.User;
import com.example.cloud.service.system.UserService;
import com.example.cloud.utils.BusinessUtil;
import com.example.cloud.utils.RequestUtil;
import org.apache.log4j.Logger;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangtao on 2018/2/3.
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = Logger.getLogger(UserServiceImpl.class);

    @Autowired
    EntityManager em;

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private MongoUserRepository mongoUserRepository;

    @Override
    public User findOneFromCache(Long id) {
        return mongoUserRepository.findOne(id);
    }

    @Override
    public User findUserByNameFromCache(String loginName) {
        return mongoUserRepository.findByUserLoginName(loginName);
    }

    @Override
    public List<User> findByAll() {
        return mongoUserRepository.findByStatusAndDeleteFlag(StateConstants.STATUS_ACTIVE, 0);
    }

    @Override
    public List<User> findHealthcareFromCache(List<Long> organIds) {
        List<Integer> jobs = new ArrayList<>();
        jobs.add(StateConstants.USER_JOB_DOCTOR);
        jobs.add(StateConstants.USER_JOB_NURSE);
        return mongoUserRepository.findByAccountIdAndOrganIdInAndJobInAndDeleteFlag(RequestUtil.getLoginUser().getAccountId(), organIds, jobs, 0);
    }

    @Override
    public List<User> findByJobFromCache(int jobType) {
        User user = RequestUtil.getLoginUser();
        return mongoUserRepository.findByAccountIdAndOrganIdAndJobAndDeleteFlag(user.getAccountId(), user.getOrganId(), jobType, 0);
    }

    @Override
    public List<User> findUserByLogis() {
        User user = RequestUtil.getLoginUser();
        List<User> list = mongoUserRepository.findByAccountIdAndOrganIdAndIsLogisticsAndDeleteFlag(user.getAccountId(), user.getOrganId(), StateConstants.IS_LOGISTICS_YES, 0);
        return list;
    }

    @Override
    public List<User> findByAccountIdFromCache() {
        return mongoUserRepository.findByAccountIdAndStatusAndDeleteFlag(RequestUtil.getLoginUser().getAccountId(), StateConstants.STATUS_ACTIVE, 0);
    }

    @Override
    public List<User> findByOrganIdFromCache(Long organId) {
        return mongoUserRepository.findByOrganIdAndStatusAndDeleteFlag(organId, StateConstants.STATUS_ACTIVE, 0);
    }

    @Override
    public List<User> findByDepartmentId(Long departmentId) {
        return mongoUserRepository.findByDepartmentIdAndStatusAndDeleteFlag(departmentId, StateConstants.STATUS_ACTIVE,0);
    }

    @Override
    @Transactional
    public void doSave(User user) {
        if(user.getId() != null){
            User u = userRepository.findOne(user.getId());
            if(user.getRoles() == null){
                user.setRoles(u.getRoles());
            }
            if(user.getOrgans() == null){
                user.setOrgans(u.getOrgans());
            }
        }
        // 保存实体
        BusinessUtil.appendRecord(user);
        userRepository.save(user);
        // 同步缓存
        mongoUserRepository.save(user);
    }

    @Override
    public void loadCache() {
        List<User> list = userRepository.findAll();
        if (!list.isEmpty()) {
            mongoUserRepository.save(list);
            logger.info("*******************用户已全部同步缓存*******************");
        }
    }

    @Override
    @Transactional
    public void doFreeze(List<Long> ids) {
        // 更新用户
        List<User> users = userRepository.findByIdIn(ids);
        for (User user : users) {
            user.setStatus(StateConstants.STATUS_FREEZE);
            BusinessUtil.appendRecord(user);
        }
        userRepository.save(users);
        // 同步缓存
        mongoUserRepository.save(users);
    }

    @Override
    @Transactional
    public void doUse(List<Long> ids) {
        // 更新用户
        List<User> users = userRepository.findByIdIn(ids);
        for (User user : users) {
            user.setStatus(StateConstants.STATUS_ACTIVE);
            BusinessUtil.appendRecord(user);
        }
        userRepository.save(users);
        // 同步缓存
        mongoUserRepository.save(users);
    }

    @Override
    @Transactional
    public void doDelete(List<Long> ids) {
        // 更新用户
        List<User> users = userRepository.findByIdIn(ids);
        for (User user : users) {
            user.setDeleteFlag(1);
            BusinessUtil.appendRecord(user);
        }
        userRepository.save(users);
        // 同步缓存
        mongoUserRepository.save(users);
    }
}
